
$(function(){
	//ID的定义
	var uploadybFuFeiDetail="uploadybFuFeiDetail";
	
	//数据上传控件部署
	uploadybFuFeiFunc(uploadybFuFeiDetail,'上传文件 ');
})
/**
 * 数据上传控件
 * @param id
 */
function uploadybFuFeiFunc(id,desc){
	$('#'+tabId+' #'+id).uploadifive({
		'uploadScript' : contextPath + '/DrgFeeMedins/drgFeeMedinsUpload.dhc',
        'height': 37,
        'width' :130,
        'auto'  : true,//是否自动开始
        'uploadLimit' : 100, //一次同步上传的文件数目     
        'fileSizeLimit' : '500MB', //单个文件大小，0为无限制，可接受KB,MB,GB等单位的字符串值     
        'queueSizeLimit' : 100, //队列中同时存在的文件个数限制
        'fileObjName':'file',
        'removeCompleted': true,  //是否完成后移除序列，默认为true
        'removeTimeout' : '5',  //上传完成后队列多长时间后消失。默认 3秒    需要：removeCompleted : true,时使用
        'progressData' :'percentage',//设置上传进度显示方式，percentage显示上传百分比，speed显示上传速度
        'buttonText' : '<i class="fa fa-folder-open fa-lg"></i>&nbsp;&nbsp;&nbsp;选择'+desc,
        'buttonClass': 'uploadifyH5_btn',
        'fileType' : '*.xls; *.xlsx; *.xml; *.dbf',//允许上传的文件格式
        'multi'    : true,//是否支持多文件上传
        'method'   :'post',//提交方式
        'debug':false,
        //当点击文件队列中文件的关闭按钮或点击取消上传时触发
        'onCancel': function(){ },
      	//返回一个错误，选择文件的时候触发  
        'onSelectError':  function(){
        	$('#navTabs').messages({
                type: 'error',
                content:  '文件上传失败！'
            }); 
        },
      	//检测FLASH失败调用  
        'onFallback':function(){
            $('#filesUp').message({
                type: 'error',
                content: '浏览器检测不到兼容版本的HTML5。 '
            });
        }, 
        'onUpload': function(filesToUpload) {
        },
        'onUploadError' : uploadify_onUploadError,
        'onQueueComplete':function(queueData){
        	closeBgDiv_ybFuFei();
        	showBgDiv_depCost();//显示遮罩层
        	getProgress_depCost(); //进度条
        	$.post(contextPath+'/DrgFeeMedins/importDrgFeeMedinsData.dhc',{"type":id}).done(function(result){
        		if(result.success){
        		}
        	});
        }
    });
}
/**
 * 关闭遮罩
 */
function closeBgDiv_ybFuFei(){
	//初始化遮罩层内容
	$('#'+tabId+' #import_loading_ybFuFei').css('display','');
	$('#'+tabId+' #ybFuFeiUpload').css('-webkit-filter','');	
	$('#'+tabId+' #ybFuFeiUpload').css('filter','');//删除模糊效果
	$('#'+tabId+' #progress_info_ybFuFei').html('');
	$('#'+tabId+' #progress_info_detail_ybFuFei').html('');	
	$('#'+tabId+' .progress-bar').css('width','0%');
	$('#'+tabId+' .progress-bar span').html('');
}
/**
 * 启动遮罩
 */
function showBgDiv_depCost(){
	$('#'+tabId+' #close_loading_btn_ybFuFei').css('display','none');
	$('#'+tabId+' #import_loading_ybFuFei').css('display','block');
	$('#'+tabId+' #ybFuFeiUpload').css('-webkit-filter','blur(8px)');
	$('#'+tabId+' #ybFuFeiUpload').css('filter','blur(8px)');
}
/**
 * 获取后台数据处理进度和详情
 */
function getProgress_depCost() {
	 timerStart();
	 var message="";
	 var timerId = setInterval(function(){
		$.get(contextPath+"/DrgFeeMedins/uploadDrgFeeMedinsProgress.dhc").done(function(result){
			var info=result.data;
			if(message!=info.msg){
				message=info.msg;
				$('#'+tabId+' #progress_info_ybFuFei').html(message);
			}
			$('#'+tabId+' #progress_info_detail_ybFuFei').html('<p>'+info.detail+'</p>');
			$('#'+tabId+' .progress-bar').css('width',info.percent+'%');
			$('#'+tabId+' .progress-bar span').html(info.percent+'%');			
			// 完成
			if (info.stateInfo=='1' ) {
				clearInterval(timeShow);
				clearInterval(timerId);
				$('#'+tabId+' #progress_info_ybFuFei').append('<p style="color:#5FBF18;"><i class="fa fa-smile-o fa-2x"></i></p>');
				$('#'+tabId+' #progress_info_ybFuFei').append('<p></p>');
				$('#'+tabId+' #close_loading_btn_ybFuFei').css('display','');
			} 
		});
	}, 500)
}
var timeShow;
function timerStart(){
	var h = 0;
	var m = 0;
	var s = 0;
	timeShow = setInterval(function(){
		s++;
		if (s==60) {
			s=0;
			m++;
		}
		if (m==60) {
			m=0;
			h++;
		}
		$('#'+tabId+' #progress_usedtime_ybFuFei').html(h+':'+m+':'+s);
	}, 1000)
}


//event_common.js

/**
 * 当点击文件队列中文件的关闭按钮或点击取消上传时触发，file参数为被取消上传的文件对象
 */
var uploadify_onCancel = function(file){
	$('#navTabs').messages({
        type: 'error',
        content:  '文件【'+ file.name + '】取消上传'
    }); 
}

/**
 * 选择文件后向队列中添加每个上传任务时如果失败都会触发
 */
var uploadify_onSelectError = function(file, errorCode, errorMsg) {  
    var msgText = "上传失败\n";  
    switch (errorCode) {  
        case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:  
            //this.queueData.errorMsg = "每次最多上传 " + this.settings.queueSizeLimit + "个文件";  
            msgText += "每次最多上传 " + this.settings.queueSizeLimit + "个文件";  
            break;  
        case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:  
            msgText += "文件大小超过限制( " + this.settings.fileSizeLimit + " )";  
            break;  
        case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:  
            msgText += "文件大小为0";  
            break;  
        case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:  
            msgText += "文件格式不正确，仅限 " + this.settings.fileTypeExts;  
            break;  
        default:  
            msgText += "错误代码：" + errorCode + "\n" + errorMsg;  
    }  
    $('#navTabs').messages({
        type: 'error',
        content:  msgText
    });
}; 

/**
 * 文件上传出错时触发，参数由服务端程序返回。
 */
var uploadify_onUploadError = function(file, errorCode, errorMsg, errorString) {  
    // 手工取消不弹出提示  
    if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED  
            || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED) {  
        return;  
    }  
    var msgText = "上传失败\n";  
    switch (errorCode) {  
        case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:  
            msgText += "HTTP 错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:  
            msgText += "上传文件丢失，请重新上传";  
            break;  
        case SWFUpload.UPLOAD_ERROR.IO_ERROR:  
            msgText += "IO错误";  
            break;  
        case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:  
            msgText += "安全性错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:  
            msgText += "每次最多上传 " + this.settings.uploadLimit + "个";  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:  
            msgText += errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:  
            msgText += "找不到指定文件，请重新操作";  
            break;  
        case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:  
            msgText += "参数错误";  
            break;  
        default:  
            msgText += "文件:" + file.name + "\n错误码:" + errorCode + "\n"  
                    + errorMsg + "\n" + errorString;  
    }  
    $('#navTabs').messages({
        type: 'error',
        content:  msgText
    });  
} 

/**
 * 当文件上传成功时触发
 */
var uploadify_onUploadSuccess = function(file, data, response) {  
	$('#navTabs').messages({
        type: 'success',
        content:  file.name + ' 上传成功！ '
    }); 
};